package com.starryx.driverschool.controller;

import com.starryx.driverschool.po.User;
import com.starryx.driverschool.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.List;


@Controller
public class AdminController {

    @Autowired
    private UserService userService;

    @GetMapping("/admin")
    public String admin(
            @PageableDefault(size = 5, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
            Model model
            ){
        model.addAttribute("page", userService.listUser(pageable));
        return "admin";
    }

    @GetMapping("/admin/input")
    public String input(Model model){
        model.addAttribute("user", new User());
        return "admin-input";
    }

    @GetMapping("/admin/input/{id}")
    public String input(@PathVariable Long id, Model model){
        model.addAttribute("user", userService.getUser(id));
        return "admin-input";
    }

    @PostMapping("/admin/save")
    public String save(@Valid User user,
                       BindingResult result,
                       RedirectAttributes attributes){
        User user1 = userService.getUserByUsername(user.getUsername());
        if(user1 != null){
            result.rejectValue("username", "usernameError", "不能添加重复的用户名");
        }
        if (result.hasErrors()){
            return "admin-input";
        }
        User u = userService.saveUser(user);
        if(u == null){
            attributes.addFlashAttribute("message", "新增失败");
        }else{
            attributes.addFlashAttribute("message", "新增成功");
        }
        return "redirect:/admin";
    }

    @PostMapping("/admin/update")
    public String update(@Valid User user,
                       BindingResult result,
                       RedirectAttributes attributes){
        User user1 = userService.getUserByUsername(user.getUsername());
        if(user1 != null && !user1.getId().equals(user.getId())){
            result.rejectValue("username", "usernameError", "已有管理员使用该用户名");
        }
        if (result.hasErrors()){
            return "admin-input";
        }
        User u = userService.updateUser(user.getId(), user);
        if(u == null){
            attributes.addFlashAttribute("message", "修改失败");
        }else{
            attributes.addFlashAttribute("message", "修改成功");
        }
        return "redirect:/admin";
    }

    @GetMapping("/admin/delete/{id}")
    public String delete(@PathVariable  Long id, RedirectAttributes attributes){
        userService.deleteUser(id);
        attributes.addFlashAttribute("message", "删除成功");
        return "redirect:/admin";
    }

    @PostMapping("/admin/batchdelete")
    public String batchDelete(@RequestParam List<Long> ids, RedirectAttributes attributes){
        for(Long id : ids){
            userService.deleteUser(id);
        }
        attributes.addFlashAttribute("message", "删除成功");
        return "redirect:/admin";
    }

}
